// DiceRoller2.java // Modified from Dietel&Deitel's Fig. 12.10: DiceRoller2.java // Creating JButtons. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class DiceRoller2 extends JFrame implements ActionListener { private JButton button1; private JCheckBox callType; private Icon images[]; private native void seedRand(); private native int getNext(); private native void setNext(JButton b); static { System.loadLibrary("DiceRoller2"); } public DiceRoller2() { super( "Testing Buttons" ); /* Seed the C random fucntion */ seedRand(); /* set up images array */ images = new Icon [7]; images[0] = new ImageIcon ("blank.gif"); images[1] = new ImageIcon ("1.gif"); images[2] = new ImageIcon ("2.gif"); images[3] = new ImageIcon ("3.gif"); images[4] = new ImageIcon ("4.gif"); images[5] = new ImageIcon ("5.gif"); images[6] = new ImageIcon ("6.gif"); Container c = getContentPane(); c.setLayout( new FlowLayout() ); // create buttons button1 = new JButton( "Click Here", images[0] ); c.add( button1 ); // create check box callType = new JCheckBox ("Call Java From C Code"); c.add (callType); // create an instance of inner class ButtonHandler // to use for button event handling button1.addActionListener( this ); setSize( 275, 100 ); show(); } public static void main( String args[] ) { DiceRoller2 app = new DiceRoller2(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); } // inner class for button event handling public void actionPerformed( ActionEvent e ) { int i; JButton b; //JOptionPane.showMessageDialog( null, //"You pressed: " + e.getActionCommand() ); b = (JButton)e.getSource(); if (callType.isSelected()) { setNext(b); } else { //i = ((int)Math.floor (Math.random()*6)) + 1; i = getNext(); setImage (b, i); } } public void setImage( JButton b, int i ) { b.setIcon(images[i]); } }